package anc.tankshooter.ui;

import java.util.ArrayList;
import java.util.Date;

import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.TiledSprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;
import org.andengine.util.color.Color;

import anc.tankshooter.GameActivity;
import anc.tankshooter.database.HighScore;
import anc.tankshooter.models.Utils;
import anc.tankshooter.ui.ConfirmScreen.ButtonDialogWatcher;
import anc.tankshooter.ui.SceneManager.SceneType;

public class HighScoreScreen extends IScreen{
	
	private Sprite mBackground;
	private Rectangle mRectBox;
	private TiledSprite mTitleHighScore;	
	private TiledSprite mBtnOk;
	private ButtonDialogWatcher mWatcher;
	
	private ArrayList<Text> mHeaderTexts;
	private ArrayList<Sprite> mIndexNumbers;
	private ArrayList<Text> mNameRows;
	private ArrayList<Text> mLevelRows;
	private ArrayList<Text> mScoreRows;
	private ArrayList<Text> mNormalTanks;
	private ArrayList<Text> mFastTanks;
	private ArrayList<Text> mHeavyTanks;
	private ArrayList<Text> mBossTanks;
	private ArrayList<Text> mTimes;
	
	public HighScoreScreen(boolean isDialog,ButtonDialogWatcher watcher){
		mWatcher=watcher;
		if(!isDialog) {
			attachChild(mBackground);
		}
		attachChild(mRectBox);
		attachChild(mTitleHighScore);
		
		for(Text t	: mHeaderTexts){
			attachChild(t);
		}
		
		for(Sprite sp: mIndexNumbers){
			attachChild(sp);
		}
		for(Text t: mNameRows){
			attachChild(t);
		}
		for(Text t: mLevelRows){
			attachChild(t);
		}
		for(Text t: mScoreRows){
			attachChild(t);
		}
		for(Text t: mNormalTanks){
			attachChild(t);
		}
		for(Text t: mFastTanks){
			attachChild(t);
		}
		for(Text t: mHeavyTanks){
			attachChild(t);
		}
		for(Text t: mBossTanks){
			attachChild(t);
		}
		for(Text t: mTimes){
			attachChild(t);
		}
		attachChild(mBtnOk); registerTouchArea(mBtnOk);
		this.setBackgroundEnabled(false);
	}
	
	@Override
	protected void createScene() {
		
		mHeaderTexts = new ArrayList<Text>();
		mIndexNumbers = new ArrayList<Sprite>();
		mNameRows = new ArrayList<Text>();
		mLevelRows = new ArrayList<Text>();
		mScoreRows = new ArrayList<Text>();
		mNormalTanks = new ArrayList<Text>();
		mFastTanks = new ArrayList<Text>();
		mHeavyTanks = new ArrayList<Text>();
		mBossTanks = new ArrayList<Text>();
		mTimes = new ArrayList<Text>();
		
		float x = GameActivity.CAMERA_WITDH/2 - 700/2;
		float y = GameActivity.CAMERA_HEIGHT/2 - 400/2;
		mBackground = new Sprite(0,0,ResourcesManager.getInstance().mBackgroundTexture,  ResourcesManager.getInstance().mVertexManager);
		mRectBox = new Rectangle(x, y, 700, 400, mVertextManager);
		mRectBox.setColor(Color.BLACK); mRectBox.setAlpha(0.6f);
		mTitleHighScore = new TiledSprite(GameActivity.CAMERA_WITDH/2 - 390/2,y,ResourcesManager.getInstance().mDialogTitlesTexture,  ResourcesManager.getInstance().mVertexManager);
		mTitleHighScore.setCurrentTileIndex(3);
	
		mBtnOk = new TiledSprite(GameActivity.CAMERA_WITDH/2-40, y+400-50,ResourcesManager.getInstance().mBtnTexture, mVertextManager){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP||pSceneTouchEvent.getAction()==TouchEvent.ACTION_OUTSIDE||
						pSceneTouchEvent.getAction()==TouchEvent.ACTION_CANCEL){
					mBtnOk.setCurrentTileIndex(12);
					if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP)
						mWatcher.onOkClick();
				}else if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN) {
					mBtnOk.setCurrentTileIndex(13);
				}
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		mBtnOk.setCurrentTileIndex(12);
		
		int padding = 25;
		y+=50;
		
		for(int i=0;i<10;i++){
			TiledSprite sp = new TiledSprite(x+20, y+50+i*padding, ResourcesManager.getInstance().mNumbersTexture,mVertextManager);
			sp.setCurrentTileIndex(i+1); sp.setSize(20,20);
			mIndexNumbers.add(sp);
			
			Text nameText = new Text(x+50, y+50+i*padding,ResourcesManager.getInstance().mWhiteFont20,
							"--------",8, mVertextManager);
			mNameRows.add(nameText);
			
			Text levelText = new Text(x+50+120, y+50+i*padding,ResourcesManager.getInstance().mWhiteFont20,
					"---",3, mVertextManager);
			mLevelRows.add(levelText);
			
			Text scoreText = new Text(x+50+120+50, y+50+i*padding,ResourcesManager.getInstance().mWhiteFont20,
					"------",6, mVertextManager);
			mScoreRows.add(scoreText);
			
			Text normalText = new Text(x+50+120+50+80, y+50+i*padding,ResourcesManager.getInstance().mWhiteFont20,
					"----",4, mVertextManager);
			mNormalTanks.add(normalText);
			
			Text fastText = new Text(x+50+120+50+80+50, y+50+i*padding,ResourcesManager.getInstance().mWhiteFont20,
					"----",4, mVertextManager);
			mFastTanks.add(fastText);
			
			Text heavyText = new Text(x+50+120+50+80+50*2, y+50+i*padding,ResourcesManager.getInstance().mWhiteFont20,
					"----",4, mVertextManager);
			mHeavyTanks.add(heavyText);
			
			Text bossText = new Text(x+50+120+50+80+50*3, y+50+i*padding,ResourcesManager.getInstance().mWhiteFont20,
					"----",4, mVertextManager);
			mBossTanks.add(bossText);
			
			Text timeText = new Text(x+50+120+50+80+50*4+10, y+50+i*padding,ResourcesManager.getInstance().mWhiteFont20,
					"--------------",14, mVertextManager);
			mTimes.add(timeText);
			
			nameText.setAlpha(0.5f); levelText.setAlpha(0.5f);scoreText.setAlpha(0.5f);
			normalText.setAlpha(0.5f); fastText.setAlpha(0.5f);heavyText.setAlpha(0.5f);bossText.setAlpha(0.5f);
			timeText.setAlpha(0.5f);
		}
		int i=-1;
		Text nameText = new Text(x+50, y+50+i*padding,ResourcesManager.getInstance().mWhiteFont18,
				"Name", mVertextManager);
		mHeaderTexts.add(nameText);
		
		Text levelText = new Text(x+50+110, y+50+i*padding,ResourcesManager.getInstance().mWhiteFont18,
				"Level", mVertextManager);
		mHeaderTexts.add(levelText);
		
		Text scoreText = new Text(x+50+120+50, y+50+i*padding,ResourcesManager.getInstance().mWhiteFont18,
				"Score",mVertextManager);
		mHeaderTexts.add(scoreText);
		
		Text normalText = new Text(x+50+120+50+80, y+50+i*padding,ResourcesManager.getInstance().mWhiteFont18,
				"N.T", mVertextManager);
		mHeaderTexts.add(normalText);
		
		Text fastText = new Text(x+50+120+50+80+50, y+50+i*padding,ResourcesManager.getInstance().mWhiteFont18,
				"F.T", mVertextManager);
		mHeaderTexts.add(fastText);
		
		Text heavyText = new Text(x+50+120+50+80+50*2, y+50+i*padding,ResourcesManager.getInstance().mWhiteFont18,
				"H.T", mVertextManager);
		mHeaderTexts.add(heavyText);
		
		Text bossText = new Text(x+50+120+50+80+50*3, y+50+i*padding,ResourcesManager.getInstance().mWhiteFont18,
				"B.T",mVertextManager);
		mHeaderTexts.add(bossText);
		
		Text timeText = new Text(x+50+120+50+80+50*4+10, y+50+i*padding,ResourcesManager.getInstance().mWhiteFont18,
				"Time",mVertextManager);
		mHeaderTexts.add(timeText);
	}
	
	public void setHighScoreData(ArrayList<HighScore> hss){
		
		for(int i=0;i<10;i++){
			mNameRows.get(i).setText("--------"); mNameRows.get(i).setAlpha(0.5f);
			mLevelRows.get(i).setText("---"); mLevelRows.get(i).setAlpha(0.5f);
			mScoreRows.get(i).setText("------"); mScoreRows.get(i).setAlpha(0.5f);
			mNormalTanks.get(i).setText("----"); mNormalTanks.get(i).setAlpha(0.5f);
			mFastTanks.get(i).setText("----"); mFastTanks.get(i).setAlpha(0.5f);
			mHeavyTanks.get(i).setText("----"); mHeavyTanks.get(i).setAlpha(0.5f);
			mBossTanks.get(i).setText("----"); mBossTanks.get(i).setAlpha(0.5f);
			mTimes.get(i).setText("------------");  mTimes.get(i).setAlpha(0.5f);
		}
		for(int i=0;i<hss.size()&&i<10;i++){
			HighScore hs = hss.get(i);
			mNameRows.get(i).setText(hs.mName); mNameRows.get(i).setAlpha(1);
			mLevelRows.get(i).setText(hs.mLevel+"");  mLevelRows.get(i).setAlpha(1);
			mScoreRows.get(i).setText(hs.mScore+"");  mScoreRows.get(i).setAlpha(1);
			mNormalTanks.get(i).setText(hs.mNormalTank+"");  mNormalTanks.get(i).setAlpha(1);
			mFastTanks.get(i).setText(hs.mFastTank+"");  mFastTanks.get(i).setAlpha(1);
			mHeavyTanks.get(i).setText(hs.mHeavyTank+"");  mHeavyTanks.get(i).setAlpha(1);
			mBossTanks.get(i).setText(hs.mBossTank+"");  mBossTanks.get(i).setAlpha(1);
			
			Date d = Utils.toDate(hs.mTime);
			mTimes.get(i).setText(d.getDate()+"-"+d.getMonth()+"-"+(d.getYear()+1900)+" "+d.getHours()+":"+d.getMinutes());  mTimes.get(i).setAlpha(1);
		}
	}

	@Override
	public void onUpdateScreen(float second) {
	}

	@Override
	public boolean onBackKeyPressed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public SceneType getSceneType() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void disposeScene() {
		
	}
}
